import { Store } from 'vuex';
import createPersistedState from 'vuex-persistedstate';

const persistedState = createPersistedState({
    'storage': window.localStorage
});

interface IState {
    userInfo: {
        secret: string
        id: string
        [key: string]: any
    }
    tagData: tagObj[]
    AdminTagData: tagObj[]
    code: string
}
const defaultState: IState = {
    'userInfo': {
        'secret': '',
        'id': ''
    },
    'code': '',
    'tagData': [],
    'AdminTagData': []
};

export default new Store({
    'plugins': [persistedState],
    'state': defaultState,
    'mutations': {
        _userInfo(state, data = {}) {
            const oldData = JSON.parse(JSON.stringify(data));
            delete oldData.auths;
            state.userInfo = oldData || {};
        },
        _logout(state, opt) {
            state.userInfo = {
                'secret': '',
                'id': '',
                'code': ''
            };
        },
        _modifyInfo(state, data = {}) {
            state.userInfo = data;
        },
        _setTagData(state, data) {
            state.tagData = data;
        }, 
        _setAdminTagData(state, data) {
            state.AdminTagData = data;
        },
        _setCode(state, data) {
            state.code = data;
        }
    },
    'actions': {
        '-userInfo': ({ commit, state }, opt) => {
            commit('_userInfo', opt);
        },
        '-logout': ({ commit, state }, opt) => {
            commit('_logout', opt);
        },
        '-_setTagData': ({ commit, state }, opt) => {
            commit('_setTagData', opt);
        },
        '-_setAdminTagData': ({ commit, state }, opt) => {
            commit('_setAdminTagData', opt);
        }
    },
    'modules': {
    }
});
